1
Foundations of Heap-Allocated Collections
AI034 Lesson 8
00:00

The Architecture of Growth

Rust’s collections, such as Vec<T> and String, are not primitive types; they are library-defined structures residing in the std module. This foundation dictates how Rust organizes data via the module system and manages memory through RAII (Resource Acquisition Is Initialization). While simple types live on the stack, collections utilize Heap Storage for dynamic growth, meaning their memory must be explicitly managed through the Drop trait.

Module Resolution & Visibility

The Rust compiler maps the module tree starting at the crate root (src/lib.rs or src/main.rs). A declaration like mod front_of_house; prompts the compiler to search for src/front_of_house.rs or src/front_of_house/mod.rs. Using pub modifiers and re-exports (pub use) allows encapsulated heap-allocated data to be safely interfaced through idiomatic paths.

File Resolution Treesrc/lib.rssrc/front_of_house.rs- hosting.rspub use crate::front_of_house;let list = Vec::new();🦀?Pitfall: mod.rs + name.rs conflicttriggers Compiler Error (Page 183)

As soon as a module’s scope ends, the Drop implementation automatically reclaims heap memory: $$Memory_{reclaimed} = \sum Drop(Elements)$$.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>